
<?php

//    (c) 2011 Donders Centre/Simon Oosthoek
//
//    This file is part of PIM.
//
//    PIM is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 2 of the License, or
//    (at your option) any later version.
//
//    PIM is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with PIM.  If not, see <http://www.gnu.org/licenses/>.
//
//read in common pim functions:

require('pimfunctions.php');

$pim_pid="0";
$pim_machine="";
$pim_owner="";
$pim_action="";
$pim_randval="";
$pim_processtype="";

if (isset($_GET['key'])) {
	$key=$_GET['key'];
	$values=base64_decode($key);
	if ($values === false) {
		print "error decoding";
	} else {
		list($pim_machine, $pim_pid, $pim_owner, $pim_action, $pim_randval) = explode(" ", $values);
		$pim_machine="mentat$pim_machine";
		$pim_action=preg_replace("/rnew/", "renew", $pim_action);
		$pim_randval=trim($pim_randval);
	}
} else {
	print_errorpage("<p>don't know what to act on</p>");
}	

$pim_datafile="$pim_datadir/${pim_machine}_$pim_pid";
if ( file_exists($pim_datafile) ) {
	$processdata=pid_data($pim_datafile);
} else {
	print_errorpage("cannot open $pim_datafile");
}
$pim_processtype=$processdata['config']['procname'];

if ( $processdata['shellview']['pid_owner'] != $pim_owner ) {
	print_errorpage("<p>user $pim_owner doesn't match owner " . $processdata['shellview']['pid_owner'] . " for this process</p>");
}	
	
switch ($pim_action) {
    case "kill":
    case "renew":
	if (strlen($pim_action) > 0) {
		if ($processdata['shellview']['pid_randval'] != $pim_randval) {
			print_errorpage("<p>error: keys don't match</p>
			<h3>Make sure you have the most recent notification e-mail!</h3> +$pim_randval+ != +" . $processdata['shellview']['pid_randval'] . "+");
		}
		$filenm = "$pim_actdir/${pim_machine}_$pim_pid";
		# write command to file
		$content = 'user_'. $pim_action . '="' . time() . "\"\n";
		$content .= "user_${pim_action}_human=\"" . strftime('%F %R') . "\"\n";
		$key=trim($pim_randval);
		$content .= "user_key=\"$key\"\n"; 
		if ( $fhandl = fopen("$filenm", "a") ) {
			if ( fwrite($fhandl, $content) ) {
				print_successpage("<h3>$pim_action process $pim_pid on $pim_machine for user $pim_owner</h3>\n<pre>written file $filenm</pre>\n");
			} else {
				print_errorpage("<p>error: error writing to action file <pre>$filenm</pre></p>");
			}
				
			fclose($fhandl);
		} else {
			print_errorpage("<p>error: cannot write to action file <pre>$filenm</pre></p>");
		}
			
	} else {
		print_errorpage("<pre>Command empty\n</pre>\n");
	}
	break;
    default:
    	print_errorpage("don't know what you mean by action <pre>\n". strip_html($act) . "</pre>");
}


/******************************************/
/* function definitions for pidaction.php */
/******************************************/

function print_errorpage($str=""){
	print_page("PIM: error processing command", $str);
	die;
}

function print_successpage($str="") {
	print_page("PIM: successfully processed command", $str);
}


function print_page($title="PIM", $str="") {
	global $processdata;
	?>
	<html>
	<head>
	<title><?php echo "$title" ?></title>
	</head>
	<body>
	<h1><a href="http://pim.fcdonders.nl/"><img 
	style="vertical-align:text-top" src="img/pim-logo.png" alt="PIM logo"/></a>
	PIM</h1>
	<?php
//	echo "<pre> " . print_r($processdata) . "\n</pre>\n";
	echo "$str";
	
	include('footer.tp');
}
?>
